package com.my.ads.lib.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.my.ads.lib.AdSummary;
import com.my.ads.lib.Config;
import com.my.ads.lib.utils.NetWorkSpeedUtils;
import com.rapid.clean.deep.R;

/**
 * Created by xuanyang on 2018/3/6.
 */

public class WifiActivity extends BaseActivity {

    private ImageView img_off, img_ad;
    private TextView text_name, text_speed;
    private RelativeLayout relative_ad_8;
    private WifiManager wifi_service;
    private WifiInfo wifiInfo;
    private Handler mHnadler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100:
                    text_speed.setText(msg.obj.toString());
                    break;
            }
            super.handleMessage(msg);
        }
    };

    @SuppressLint("WifiManagerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi);
        initViews();
        other();
        showAd();
        click();
    }

    private void other() {
        wifi_service = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        wifiInfo = wifi_service.getConnectionInfo();
        text_name.setText(wifiInfo.getSSID());

        new NetWorkSpeedUtils(this, mHnadler).startShowNetSpeed();
    }

    private void showAd() {
        AdSummary.getInstance().showNativeAd(this, relative_ad_8, false);
    }

    private void click() {
        img_off.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                finish();
            }
        });

        img_ad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent in = new Intent(WifiActivity.this, GiftActivity.class);
                startActivity(in);
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onResume() {
        super.onResume();

        SharedPreferences sp_1 = getSharedPreferences("native_ad", Context.MODE_PRIVATE);
        Long show_time = sp_1.getLong("show_native_ad_time", 0L);
        // 刷新原生广告
        if (show_time != 0L && ((System.currentTimeMillis() - show_time) > Integer.valueOf(Config.update_native_ad) * 1000)) {
            try {
                AdSummary.getInstance().showNativeAd(this, relative_ad_8, false);
            } catch (Exception e) {
            }
        }
    }

    private void initViews() {
        img_ad = (ImageView) findViewById(R.id.img_ad);
        img_off = (ImageView) findViewById(R.id.img_off);
        text_speed = (TextView) findViewById(R.id.text_speed);
        text_name = (TextView) findViewById(R.id.text_name);
        relative_ad_8 = (RelativeLayout) findViewById(R.id.relative_ad_8);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }
}
